<?php

Class BankPayment extends Payment {
    private static $singular_name = "Bank Payment";
    private static $plural_name = "Bank Payments";
    
    private static $db = array(
        'BankName' => 'Varchar(100)',
        'BranchName' => 'Varchar(100)',
        'AccountName' => 'Varchar(250)',
        'AccountNumber' => 'Varchar(100)',
        'TransactionID' => 'Varchar(100)',
        'TransactionDate' => 'Date',
        'TransactionTime' => 'Time'
    );
	
	public function fieldLabels($includerelations = true) {
		$labels = parent::fieldLabels($includerelations);
		
		$labels['BankName'] = _t('BankPayment.BANK_NAME', 'Bank Name');
		$labels['BranchName'] = _t('BankPayment.BRANCH_NAME', 'Branch Name');
		$labels['AccountName'] = _t('BankPayment.ACCOUNT_NAME', 'Account Name');
		$labels['AccountNumber'] = _t('BankPayment.ACCOUNT_NUMBER', 'Account No.');
		$labels['TransactionID'] = _t('BankPayment.TRANSACTION_ID', 'Transaction ID');
		$labels['TransactionDate'] = _t('BankPayment.TRANSACTION_DATE', 'Transaction Date');
		$labels['TransactionTime'] = _t('BankPayment.TRANSACTION_TIME', 'Transaction Time');
		
		return $labels;	
	}
	
	function validate() {
        $validationResult = parent::validate();

		if($this->isChanged('Status') && $this->Status == 'Success'){
			$data = array(
				'BankPayment_BankName' => $this->BankName,
				'BankPayment_AccountName' => $this->AccountName,
				'BankPayment_AccountNumber' => $this->AccountNumber
			);
	        $errors = $this->getPaymentFormRequirements(RequiredFields::create(), $data)->getErrors();

			if($errors){
	        	foreach($errors as $error){
            		$validationResult->error($error['message']);
				}
            }

			if($this->TransactionID == '' || $this->TransactionDate == '' || $this->TransactionTime == ''){
				$validationResult->error(_t('BankPayment.BANK_REFERENCE_REQUIRED', 'Please register bank reference'));
			}
		}

        return $validationResult;
    }
    
    function getCMSFields(){
        $fields = parent::getCMSFields();
        
        $fields->makeFieldReadonly('BankName');
        $fields->makeFieldReadonly('BranchName');
        $fields->makeFieldReadonly('AccountName');
        $fields->makeFieldReadonly('AccountNumber');
        $fields->makeFieldReadonly('TransactionID');
        $fields->makeFieldReadonly('TransactionDate');
		$fields->makeFieldReadonly('TransactionTime');
        
        return $fields;
    }
    
    function getPaymentFormFields($memberid){
        $fields = FieldList::create(
			TextField::create('BankPayment_BankName', $this->fieldLabel('BankName')),
			TextField::create('BankPayment_BranchName', $this->fieldLabel('BranchName')),
			TextField::create('BankPayment_AccountName', $this->fieldLabel('AccountName')),
			TextField::create('BankPayment_AccountNumber', $this->fieldLabel('AccountNumber'))
		);
        
		$this->extend('updatePaymentFormFields', $fields, $memberid);
		
        return $fields;
    }

    function getPaymentFormRequirements($validator, $data){
        if($data['BankPayment_BankName'] == ''){
            $validator->validationError(
                'BankPayment_BankName',
                _t('BankPayment.BANK_NAME_REQUIRED', 'Bank Name is required'),
                'required'
            );
        }
        
        if($data['BankPayment_AccountName'] == ''){
            $validator->validationError(
                'BankPayment_AccountName',
                _t('BankPayment.ACCOUNT_NAME_REQUIRED', 'Account Name is required'),
                'required'
            );
        }
        
        if($data['BankPayment_AccountNumber'] == ''){
            $validator->validationError(
                'BankPayment_AccountNumber',
                _t('BankPayment.ACCOUNT_NUMBER_REQUIRED', 'Account Number is required'),
                'required'
            );
        }
        
		$this->extend('updatePaymentFormRequirements', $validator, $data);
		
        return $validator;
    }
	
	function canApprove($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
		
		if($this->exists() && ($this->Status == 'Incomplete' || $this->Status == 'Process')){
			return Permission::check('APPROVE_Payment');
		}
        return false;
    }
    
    function pendingPayment($data = array()){
    	if($this->Status == '' || $this->Status == 'Incomplete'){
        	$this->update($data)->setField('Status', 'Pending')->write();
		}
        return $this;
    }
    
    function processPayment($data = array()){
    	if($this->Status == '' || $this->Status == 'Incomplete' || $this->Status == 'Pending'){
        	$this->update($data)->setField('Status', 'Process')->write();
		}
        return $this;
    }
    
    function declinePayment($data = array()){
    	if($this->Status == '' || $this->Status == 'Incomplete' || $this->Status == 'Pending' || $this->Status == 'Process'){
        	$this->update($data)->setField('Status', 'Declined')->write();
		}
        return $this;
    }
    
    function completePayment($data = array()){
    	if($this->Status == '' || $this->Status == 'Incomplete'){
    		$this->update($data)->setField('Status', 'Pending')->write();
    	}
		else if($this->Status == 'Process'){
        	$this->update($data)->setField('Status', 'Success')->write();
		}
        return $this;
    }
    
    function ProcessLink(){
        $token = SecurityToken::inst();
        return $token->addToUrl(Controller::join_links('bankwire', 'process', $this->ID)) . sprintf('&locale=%s', Controller::curr()->Locale);
    }
    
    function getAction(){
        if($this->Status == 'Pending' && $this->Receipt()->Status != 'Voided') {
            $token = SecurityToken::inst();
            $link = $token->addToUrl(Controller::join_links('bankwire', 'register_bank_reference', $this->ID));
            $action = '<li><a title="'._t('BankPayment.CLICK_REGISTER_BANK_REFERENCE', 'Click here to register your bank reference').'" data-title="'._t('BankPayment.REGISTER_BANK_REFERENCE', 'Register Bank Reference').'" rel="popup tooltip" data-modal-lg="1" href="' . $link . '">'._t('BankPayment.REGISTER_BANK_REFERENCE', 'Register Bank Reference').'</a></li>';
            return $action;
        }
    }
    
    function onBeforeWrite(){
        parent::onBeforeWrite();
        
        if($this->TransactionID != '' && $this->TransactionDate != '' && $this->TransactionTime != '' && $this->Status == 'Pending'){
            $this->Status = 'Process';
        }
    }
}

?>